// Copyright (C) 1953-2022 NUDT
// Verilog module name - announce_process
// Version: V4.1.0.20221208
// Created:
//         by - fenglin
////////////////////////////////////////////////////////////////////////////
// Description:
//         
///////////////////////////////////////////////////////////////////////////

`timescale 1ns/1ps

module announce_process
#(
parameter local_module_id = 12'd0,
parameter clk_period = {8'd8,41'h0}//8ns
)
(
    i_clk  ,
    i_rst_n,
    
    i_gm_role   ,
	iv_announce_period   ,
    i_tsnnic_or_tsnswitch,

	iv_hcp_mid            ,
    
    iv_data               ,
    i_data_wr             ,
  
	ov_data               ,
	o_data_wr                                              
);

// I/O
// clk & rst
input                   i_clk;
input                   i_rst_n; 
// pkt input
input                   i_gm_role;
input     [3:0]         iv_announce_period  ;
input                   i_tsnnic_or_tsnswitch;

input     [11:0]        iv_hcp_mid     ;

input     [8:0]	        iv_data        ;
input   	            i_data_wr      ;
// pkt output to NMA
output    [8:0]	        ov_data        ;
output   	            o_data_wr      ;
                						          
wire	                w_announce_generate_pulse_apt2apg ;
wire                    w_first_byte_valid_apg2ope    ;

wire       [8:0]        wv_data_apg2ope;           
wire                    w_data_wr_apg2ope;
wire       [3:0]        wv_ptp_message_type_apg2ope; 

wire       [8:0]        wv_data_ats2mux    ; 
wire                    w_data_wr_ats2mux  ;

wire       [8:0]        wv_data_ope2mux    ; 
wire                    w_data_wr_ope2mux  ;

wire       [8:0]        wv_data_app2ats    ; 
wire                    w_data_wr_app2ats  ;
announce_packet_process announce_packet_process_inst
(
        .i_clk              (i_clk                   ),
        .i_rst_n            (i_rst_n                 ),
                                                     
        .iv_data            (iv_data                 ),
        .i_data_wr          (i_data_wr               ),
                            
        .ov_data            (wv_data_app2ats         ),
		.o_data_wr          (w_data_wr_app2ats       )
);

sync_transmit_select announce_transmit_select_inst
(
    .i_clk                 (i_clk                ),
    .i_rst_n               (i_rst_n              ),
                            
	.i_tsnnic_or_tsnswitch (i_tsnnic_or_tsnswitch),//1:tsnnic.  0:tsnswitch
                            
    .iv_data               (wv_data_app2ats    ),
	.i_data_wr             (w_data_wr_app2ats  ),	
                            
	.ov_data               (wv_data_ats2mux      ),
	.o_data_wr             (w_data_wr_ats2mux    )
);
   
sync_period_timing  
#(
.clk_period(clk_period)
)
announce_period_timing_inst
(
        .i_clk                  (i_clk  ),
        .i_rst_n                (i_rst_n),
 
        .i_gm_role	            (i_gm_role           ),
        .iv_sync_period         (iv_announce_period  ),		
 
        .o_sync_generate_pulse  (w_announce_generate_pulse_apt2apg)
);

announce_packet_generate 
#(
.local_module_id(local_module_id)
)
announce_packet_generate_inst
(
    .i_clk                  (i_clk   ),
    .i_rst_n                (i_rst_n ),
	 
	.i_announce_generate_pulse  (w_announce_generate_pulse_apt2apg),
	.iv_hcp_mid                 (iv_hcp_mid                       ),
 
	.ov_data                (wv_data_apg2ope             ),
	.o_data_wr              (w_data_wr_apg2ope           ),
    .ov_ptp_message_type    (wv_ptp_message_type_apg2ope ),
    .o_first_byte_valid     (w_first_byte_valid_apg2ope  )    
);

opensync_protocol_encapsulate 
#(
.osm_id(8'd0),
.local_module_id(local_module_id)
)
opensync_protocol_encapsulate_anp
(
        .i_clk              (i_clk            ),
        .i_rst_n            (i_rst_n          ),

        .iv_hcp_mid         (iv_hcp_mid             ),
        .iv_opensync_dst_module_id(12'h700),//组播的moduleid.
        
        .iv_data            (wv_data_apg2ope       ),
        .i_data_wr          (w_data_wr_apg2ope     ),
		//.iv_osm_id          (8'b0        ),
		.iv_eth_type        (16'h88f7                    ),
		.iv_ptp_messagetype (wv_ptp_message_type_apg2ope ),
        .iv_local_count_rx  (40'b0                       ),
        .i_diagest_wr       (w_first_byte_valid_apg2ope  ),
        
        .ov_data            (wv_data_ope2mux    ),
        .o_data_wr          (w_data_wr_ope2mux  )
);

grandmaster_slave_select announce_select_inst
(
    .i_clk          (i_clk        ),
    .i_rst_n        (i_rst_n      ),
                     
	.i_gm_role      (i_gm_role    ),
                     
    .iv_data_gnp    (wv_data_ope2mux  ),
	.i_data_wr_gnp  (w_data_wr_ope2mux),
	                 
    .iv_data_snp    (wv_data_ats2mux  ),
	.i_data_wr_snp  (w_data_wr_ats2mux),	
	                 
	.ov_data        (ov_data      ),
	.o_data_wr      (o_data_wr    )
);                
endmodule